/* 获取随机颜色 */
getRandomColor = function(){
	var args = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'];
	var color="#";
	for (var i = 0 ;i<6 ;i++  ) {
		color +=args[Math.round(Math.random()*16)];
	}
	return color;
}

RgbToColorString = function(r,g,b){
	
	var color = "#";
	toColorChar = function(num){
		var args = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'];
		var a = parseInt(num/16);
		var s = "";
		var b = num - a*16;
		s = args[a] + args[b];
		return s;
	}
    color = color + toColorChar(r) +　toColorChar(g)+ toColorChar(b);
	return color;
}



   /*
	 参数：
	 startColor：开始颜色hex
	 endColor：结束颜色hex
     step:几个阶级（几步）
   */
//		颜色渐变  
gradientColor = function(startColor,endColor,step){
       startRGB = colorRgb(startColor);//转换为rgb数组模式
       startR = startRGB[0];
       startG = startRGB[1];
       startB = startRGB[2];

       endRGB = colorRgb(endColor);
       endR = endRGB[0];
       endG = endRGB[1];
       endB = endRGB[2];

       sR = (endR-startR)/step;//总差值
       sG = (endG-startG)/step;
       sB = (endB-startB)/step;

       var colorArr = [];
       for(var i=0;i<step;i++){
		   //计算每一步的hex值 
           var hex = colorHex('rgb('+parseInt((sR*i+startR))+','+parseInt((sG*i+startG))+','+parseInt((sB*i+startB))+')');
           colorArr.push(hex);
       }
       return colorArr;
   }

// 将hex表示方式转换为rgb表示方式(这里返回rgb数组模式)
colorRgb = function(sColor){
       var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
       var sColor = sColor.toLowerCase();
       if(sColor && reg.test(sColor)){
           if(sColor.length === 4){
               var sColorNew = "#";
               for(var i=1; i<4; i+=1){
                   sColorNew += sColor.slice(i,i+1).concat(sColor.slice(i,i+1));
               }
               sColor = sColorNew;
           }
           //处理六位的颜色值
           var sColorChange = [];
           for(var i=1; i<7; i+=2){
               sColorChange.push(parseInt("0x"+sColor.slice(i,i+2)));
           }
           return sColorChange;
       }else{
           return sColor;
       }
   };

//		 将rgb表示方式转换为hex表示方式
colorHex = function(rgb){
       var _this = rgb;
       var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
       if(/^(rgb|RGB)/.test(_this)){
           var aColor = _this.replace("rgb","").replace("RGB","").replace("(","").replace(")","").split(",");
           var strHex = "#";
           for(var i=0; i<aColor.length; i++){
               var hex = Number(aColor[i]).toString(16);
               hex = hex<10 ? 0+''+hex :hex;// 保证每个rgb的值为2位
               if(hex === "0"){
                   hex += hex;
               }
               strHex += hex;
           }
           if(strHex.length !== 7){
               strHex = _this;
           }
           return strHex;
       }else if(reg.test(_this)){
           var aNum = _this.replace(/#/,"").split("");
           if(aNum.length === 6){
               return _this;
           }else if(aNum.length === 3){
               var numHex = "#";
               for(var i=0; i<aNum.length; i+=1){
                   numHex += (aNum[i]+aNum[i]);
               }
               return numHex;
           }
       }else{
           return _this;
       }
   }


/***** 
  setColorRegular = function(obj,time){
	
	var index = [0 ,0 ,0];
	indexAdd = function(i){
		index[i]++;
		if( index[i] >=16 ){
			index[i] = 15;
			if(i > 0){
				indexAdd(i-1);
				for(var k = i; k < 6; k++){
					index[k]=0;
				}
			}else{
				index = [0 ,0 ,0 ,0 ,0 ,0];
				return;
			}
		}
	}
	return setInterval(function(){
		indexAdd(5);
		for (var i = 0 ;i<6 ;i++  ) {
		color +=args[index[i]];
		}
		obj.css({"background-color":color});
		color = "#";
	},time);
} 
*/


/*	
 * 
 * 设置对象背景颜色渐变
 * Author : king
 * 参数: obj : 渐变对象
 *		time : 渐变时间
 */
setColorRegular = function(obj,time){
	var colorArr = ["#CC9999" , "#CCCC99" , "#9ECC99" , "#99CCCA",  "#999ECC"  , "#CC99CB" 	,"#CC999D"];
	var gradiantColorArr = [];
	for (var i = 0 ; i < colorArr.length-1; i++  ) {
		gradiantColorArr = gradiantColorArr.concat(gradientColor(colorArr[i] , colorArr[i+1] ,20));
	}
	var reg = 0;
	
	return setInterval(function(){
		
		obj.css({"background-color":gradiantColorArr[reg]});
		reg++;
		if(reg > gradiantColorArr.length){
			reg = 0;
		}
	},time);
}